package com.sanji.sjmallwidget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.sanji.sjmallapp.R;
import com.sanji.sjmallutils.Tools;

/**
 * 商品 库存-数量 校验
 * Created by zsf on 17-2-18.
 */
public class NumControlView extends LinearLayout implements View.OnClickListener{

    private ImageView reduceImage;//减少
    private ImageView addImage;//增加
    private EditText goodsNumberEdit;//显示数量
    private View view;

    private int goods_count = 0;//购买数量
    private int goods_storage = 0;//商品库存
    private int goods_limit = 0;//商品限购

    private OnNumChangeListener mChangeListener;

    public NumControlView(Context context) {
        super(context);
    }

    public NumControlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        view = LayoutInflater.from(context).inflate(R.layout.activity_goods_add_reduce_linear, this, true);
        reduceImage = (ImageView) view.findViewById(R.id.image_reduce);
        addImage = (ImageView) view.findViewById(R.id.image_add);
        goodsNumberEdit = (EditText) view.findViewById(R.id.edit_number_tv);
        reduceImage.setOnClickListener(this);
        addImage.setOnClickListener(this);
    }

    public void setOnNumChangeListener(OnNumChangeListener onNumChangeListener) {
        this.mChangeListener = onNumChangeListener;
    }

    /**
     * 设置商品参数
     * @param goodsStorage  库存
     * @param goodsLimit    限购数量（-1表示不限购）
     * @param count    当前数量
     * @param isSelect    当前是否选中
     */
    public void setGoodsParameter(int goodsStorage,int goodsLimit,int count,boolean isSelect) {
        this.goods_storage = goodsStorage;
        this.goods_limit = goodsLimit;
        this.goods_count = count;
        this.goodsNumberEdit.setText(String.valueOf(count));
        this.goodsNumberEdit.setTag(isSelect);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.image_reduce:
                //数量 -
                if (goods_count > 1){
                    int num = goods_count - 1;
                    if (mChangeListener != null){
                        mChangeListener.onNumChange(this,num);
                    }

                }else{
                    Tools.toastTools(getContext(), "不能再少了");
                }
                break;
            case R.id.image_add:
                //数量 +
                if ((goods_limit > 0 && goods_count < goods_limit) || (goods_limit < 0 && goods_count < goods_storage)) {
                    int num = goods_count + 1;
                    if (mChangeListener != null){
                        mChangeListener.onNumChange(this,num);
                    }
                } else {
                    Tools.toastTools(getContext(), "不能再多了");
                }
                break;
        }
    }




    //同步数量并显示
    public void synchroNumEt(int num){
        goods_count = num;
        this.goodsNumberEdit.setText(String.valueOf(goods_count));
    }






    public interface OnNumChangeListener {
        void onNumChange(View view, int num);
    }
}
